package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.MemberEntity;
import com.hy.admin.helper.request.AccountRequest;
import com.hy.admin.service.AccountService;
import com.hy.config.ConfigurationUtils;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by shitou on 17/12/27.
 */
@RestController
@RequestMapping("/api/authen")
public class AuthenController {

    protected final Logger logBase = LoggerFactory.getLogger(MemberController.class);
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ApiResponseDTO login(@RequestBody final ApiRequestDTO apiRequestDTO) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("login_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        AccountRequest accountRequest = gson.fromJson(apiRequestDTO.getRequestData(), AccountRequest.class);
        ServicesResult<MemberEntity> accountResponse = accountService.login(accountRequest);
        apiResponseDTO.initServicesResult(accountResponse);
        apiResponseDTO.setResponseData(gson.toJson(accountResponse.getResult()));
        logBase.info("login_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
}
